cmake_minimum_required(VERSION 3.10)
project(CKKS)

# 设置 C++ 标准
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

set(CMAKE_BUILD_TYPE Release)

# Release 编译选项 (优化级别设置为 -O3)
set(CMAKE_CXX_FLAGS_RELEASE "-O3")
set(CMAKE_C_FLAGS_RELEASE "-O3")

# ================== 查找 OpenMP ==================
find_package(OpenMP REQUIRED)

if(OpenMP_CXX_FOUND)
    message(STATUS "OpenMP found")
    # 添加 OpenMP 编译选项
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}")
else()
    message(WARNING "OpenMP not found")
endif()

find_path(GMP_INCLUDE_DIR
        NAMES gmp.h
        PATHS /opt/homebrew/include /usr/local/include /usr/include
        NO_DEFAULT_PATH
)

find_library(GMP_LIB
        NAMES gmp
        PATHS /opt/homebrew/lib /usr/local/lib /usr/lib
        NO_DEFAULT_PATHs
)

find_library(GMPXX_LIB
        NAMES gmpxx
        PATHS /opt/homebrew/lib /usr/local/lib /usr/lib
        NO_DEFAULT_PATH
)

file(GLOB LIB_SOURCES "src/*.cpp")

list(FILTER LIB_SOURCES EXCLUDE REGEX "main\\.cpp$")

add_library(maintest STATIC ${LIB_SOURCES})

target_include_directories(maintest PUBLIC
        ${GMP_INCLUDE_DIR}
)

target_link_libraries(maintest PRIVATE
        ${GMP_LIB}
        ${GMPXX_LIB}
)

aux_source_directory(. SOURCES)

add_executable(CKKS ${SOURCES}
        main.cpp
        src/bootstrapping/bootstrapping.cpp
        src/bootstrapping/bootstrapping.h
        src/bootstrapping/cosine_approx.cpp
        src/bootstrapping/cosine_approx.h
        src/bootstrapping/homomorphic_encoding.cpp
        src/bootstrapping/homomorphic_encoding.h
        src/bootstrapping/homomorphic_mod.cpp
        src/bootstrapping/homomorphic_mod.h
        src/ciphertext.cpp
        src/ciphertext.h
        src/common.h
        src/context.cpp
        src/context.h
        src/evaluator_utils.cpp
        src/evaluator_utils.h
        src/key.cpp
        src/key.h
        src/numb.cpp
        src/numb.h
        src/plaintext.cpp
        src/plaintext.h
        src/scheme.cpp
        src/scheme.h
        src/secret_key.cpp
        src/secret_key.h
        src/string_utils.cpp
        src/string_utils.h
        src/test_scheme.cpp
        src/test_scheme.h
        src/time_utils.cpp
        src/time_utils.h
        src/parameters.cpp)

target_link_libraries(CKKS PRIVATE
        maintest
        -lgmp -lntl
        ${GMP_LIB}
        ${GMPXX_LIB}
)
